import { constantRoutes, asyncRoutes } from '@/router'

const state = {
  routes: constantRoutes // 默认静态路由
}
const mutations = {
  setRoutes(state, newRoutes) { // 动态路由， 将静态路由与角色拥有权限的路由结合
    state.routes = [...constantRoutes, ...newRoutes]
  }
}
const actions = {
  filterRoutes(context, menus) { // 对动态路由权限数组进行筛选
    // console.log('进入筛选函数')
    const routes = []
    // console.log('asyncRoutes', asyncRoutes)
    // console.log('menus', menus)
    menus.forEach(key => {
      routes.push(...asyncRoutes.filter(item => item.name === key)) // 查找符合条件的数组，并进行追加
    })
    // console.log(routes)
    context.commit('setRoutes', routes) // 将符合条件我的数组，提交给同步函数，并改变存储中的 routes
    return routes // 返回结果，为后面addRoutes做准备
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
